<template>
	<view class="profile-container">
		<!-- 头像设置区域 -->
		<view class="avatar-section">
			<text class="section-label">头像设置</text>
			<view class="avatar-container">
				<image 
					class="avatar-image" 
					:src="userInfo.avatar" 
					mode="aspectFill"
					@click="changeAvatar"
				></image>
				<view class="camera-icon" @click="changeAvatar">
					<text class="camera-symbol">📷</text>
				</view>
			</view>
			<text class="avatar-tip">点击更换头像,支持jpg、png格式,大小不超过2MB</text>
		</view>
		
		<!-- 昵称设置区域 -->
		<view class="nickname-section">
			<text class="section-label">昵称</text>
			<input 
				class="nickname-input" 
				v-model="userInfo.nickname" 
				placeholder="请输入昵称"
				maxlength="12"
			/>
			<text class="input-tip">最多可输入12个字符,支持中英文、数字及下划线</text>
		</view>
		
		<!-- 性别选择区域 -->
		<view class="gender-section">
			<text class="section-label">性别</text>
			<view class="gender-options">
				<view 
					class="gender-option" 
					:class="{ active: userInfo.gender === 'male' }"
					@click="selectGender('male')"
				>
					<text class="option-icon" :class="{ active: userInfo.gender === 'male' }">
						{{ userInfo.gender === 'male' ? '✓' : '○' }}
					</text>
					<text class="option-text">男</text>
				</view>
				<view 
					class="gender-option" 
					:class="{ active: userInfo.gender === 'female' }"
					@click="selectGender('female')"
				>
					<text class="option-icon" :class="{ active: userInfo.gender === 'female' }">
						{{ userInfo.gender === 'female' ? '✓' : '○' }}
					</text>
					<text class="option-text">女</text>
				</view>
				<view 
					class="gender-option" 
					:class="{ active: userInfo.gender === 'private' }"
					@click="selectGender('private')"
				>
					<text class="option-icon" :class="{ active: userInfo.gender === 'private' }">
						{{ userInfo.gender === 'private' ? '✓' : '○' }}
					</text>
					<text class="option-text">保密</text>
				</view>
			</view>
		</view>
		
		<!-- 手机号码区域 -->
		<view class="phone-section">
			<text class="section-label">手机号码</text>
			<view class="phone-container">
				<text class="phone-number">{{ userInfo.phone }}</text>
				<text class="modify-btn" @click="modifyPhone">修改</text>
			</view>
		</view>
		
		<!-- 个性签名区域 -->
		<view class="signature-section">
			<text class="section-label">个性签名</text>
			<textarea 
				class="signature-input" 
				v-model="userInfo.signature" 
				placeholder="请输入个性签名,最多可输入30个字符"
				maxlength="30"
			></textarea>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 用户信息数据
				userInfo: {
					nickname: '张先生',
					avatar: '/static/logo.png',
					gender: 'male',
					phone: '138****5678',
					signature: ''
				}
			}
		},
		
		onLoad() {
			// 页面加载时获取用户信息
			this.loadUserInfo()
		},
		
		onShow() {
			// 页面显示时刷新数据
			this.loadUserInfo()
		},
		
		// 页面右上角保存按钮
		onNavigationBarButtonTap(e) {
			if (e.index === 0) {
				this.saveProfile()
			}
		},
		
		methods: {
			/**
			 * 加载用户信息
			 * 从本地存储或API获取用户信息
			 */
			loadUserInfo() {
				const userInfo = uni.getStorageSync('user_info')
				if (userInfo) {
					this.userInfo = { ...this.userInfo, ...userInfo }
				}
			},
			
			/**
			 * 更换头像
			 */
			changeAvatar() {
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success: (res) => {
						const tempFilePath = res.tempFilePaths[0]
						
						// 检查文件大小
						uni.getFileInfo({
							filePath: tempFilePath,
							success: (fileInfo) => {
								if (fileInfo.size > 2 * 1024 * 1024) {
									uni.showToast({
										title: '图片大小不能超过2MB',
										icon: 'none'
									})
									return
								}
								
								// 更新头像
								this.userInfo.avatar = tempFilePath
								uni.showToast({
									title: '头像更新成功',
									icon: 'success'
								})
							}
						})
					},
					fail: (err) => {
						console.error('选择图片失败:', err)
					}
				})
			},
			
			/**
			 * 选择性别
			 * @param {String} gender 性别值
			 */
			selectGender(gender) {
				this.userInfo.gender = gender
			},
			
			/**
			 * 修改手机号
			 */
			modifyPhone() {
				uni.showModal({
					title: '修改手机号',
					content: '此功能需要验证当前手机号，是否继续？',
					success: (res) => {
						if (res.confirm) {
							uni.showToast({
								title: '手机号修改功能开发中',
								icon: 'none'
							})
						}
					}
				})
			},
			
			/**
			 * 保存个人信息
			 */
			saveProfile() {
				// 验证昵称
				if (!this.userInfo.nickname.trim()) {
					uni.showToast({
						title: '请输入昵称',
						icon: 'none'
					})
					return
				}
				
				// 验证昵称格式
				const nicknameRegex = /^[a-zA-Z0-9\u4e00-\u9fa5_]{1,12}$/
				if (!nicknameRegex.test(this.userInfo.nickname)) {
					uni.showToast({
						title: '昵称格式不正确',
						icon: 'none'
					})
					return
				}
				
				uni.showLoading({
					title: '保存中...'
				})
				
				// 模拟保存到本地存储
				setTimeout(() => {
					uni.setStorageSync('user_info', this.userInfo)
					uni.hideLoading()
					uni.showToast({
						title: '保存成功',
						icon: 'success'
					})
					
					// 延迟返回上一页
					setTimeout(() => {
						uni.navigateBack()
					}, 1500)
				}, 1000)
				
				// 实际项目中应该调用后端API
				// this.updateUserProfile()
			},
			
			/**
			 * 更新用户资料到服务器
			 */
			updateUserProfile() {
				uni.request({
					url: getApp().globalData.apiPath + '/user/profile',
					method: 'PUT',
					header: {
						'Authorization': 'Bearer ' + uni.getStorageSync('tenant_token')
					},
					data: {
						nickname: this.userInfo.nickname,
						gender: this.userInfo.gender,
						signature: this.userInfo.signature
					},
					success: (res) => {
						if (res.data.code === 200) {
							uni.setStorageSync('user_info', this.userInfo)
							uni.showToast({
								title: '保存成功',
								icon: 'success'
							})
							setTimeout(() => {
								uni.navigateBack()
							}, 1500)
						} else {
							uni.showToast({
								title: res.data.msg || '保存失败',
								icon: 'none'
							})
						}
					},
					fail: (err) => {
						console.error('保存失败:', err)
						uni.showToast({
							title: '网络错误，请重试',
							icon: 'none'
						})
					}
				})
			}
		}
	}
</script>

<style scoped>
	.profile-container {
		width: 100%;
		min-height: 100vh;
		background-color: #f5f5f5;
		padding: 30rpx;
	}
	
	/* 通用区域样式 */
	.avatar-section,
	.nickname-section,
	.gender-section,
	.phone-section,
	.signature-section {
		background-color: #ffffff;
		border-radius: 16rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;
	}
	
	.section-label {
		display: block;
		font-size: 32rpx;
		color: #333333;
		font-weight: bold;
		margin-bottom: 20rpx;
	}
	
	/* 头像设置区域 */
	.avatar-container {
		position: relative;
		display: inline-block;
		margin-bottom: 20rpx;
	}
	
	.avatar-image {
		width: 120rpx;
		height: 120rpx;
		border-radius: 50%;
	}
	
	.camera-icon {
		position: absolute;
		bottom: 0;
		right: 0;
		width: 40rpx;
		height: 40rpx;
		background-color: #3CC45F;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		border: 4rpx solid #ffffff;
	}
	
	.camera-symbol {
		font-size: 20rpx;
		color: #ffffff;
	}
	
	.avatar-tip {
		font-size: 24rpx;
		color: #999999;
		line-height: 1.5;
	}
	
	/* 昵称设置区域 */
	.nickname-input {
		width: 100%;
		height: 80rpx;
		background-color: #f8f8f8;
		border-radius: 8rpx;
		padding: 0 20rpx;
		font-size: 28rpx;
		color: #333333;
		margin-bottom: 16rpx;
	}
	
	.input-tip {
		font-size: 24rpx;
		color: #999999;
	}
	
	/* 性别选择区域 */
	.gender-options {
		display: flex;
		gap: 20rpx;
	}
	
	.gender-option {
		display: flex;
		align-items: center;
		padding: 16rpx 24rpx;
		background-color: #f8f8f8;
		border-radius: 8rpx;
		border: 2rpx solid transparent;
		/* transition: all 0.3s ease; */
	}
	
	.gender-option.active {
		background-color: #E3F2FD;
		border-color: #3CC45F;
	}
	
	.option-icon {
		font-size: 24rpx;
		color: #CCCCCC;
		margin-right: 12rpx;
	}
	
	.option-icon.active {
		color: #3CC45F;
	}
	
	.option-text {
		font-size: 28rpx;
		color: #333333;
	}
	
	/* 手机号码区域 */
	.phone-container {
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 80rpx;
		background-color: #f8f8f8;
		border-radius: 8rpx;
		padding: 0 20rpx;
	}
	
	.phone-number {
		font-size: 28rpx;
		color: #333333;
	}
	
	.modify-btn {
		font-size: 28rpx;
		color: #3CC45F;
	}
	
	/* 个性签名区域 */
	.signature-input {
		width: 100%;
		min-height: 120rpx;
		background-color: #f8f8f8;
		border-radius: 8rpx;
		padding: 20rpx;
		font-size: 28rpx;
		color: #333333;
		line-height: 1.5;
	}
</style>
